<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fetch_vipshop.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-02.
//  Copyright 2011 lishuzu. All rights reserved.
// 

/**
* 抓取vipshop数据
*/
class Vipshop_model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->library('Snoopy');
		$this->load->library('simple_html_dom', '', 'dom');
		$this->snoopy->agent = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1';
		$this->snoopy->referer = 'http://www.baidu.com';
		//超时 设置为不超时
		$this->snoopy->read_timeout = 0;
		$this->snoopy->rawheaders["Pragma"] = "no-cache";
		$this->snoopy->rawheaders["X_FORWARDED_FOR"] = '220.181.111.147';
		//抓取商品数量
		$this->fetch_goods_num = 5;
		//活动表
		$this->activity_table = 'vipshop_activity';
		//商品表
		$this->goods_table = 'vipshop_goods';
		//抓取网站链接
		$this->fetchurl = 'http://www.vipshop.com/';
	}
	
	function fetch(){
		$this->fetchurl = 'http://www.vipshop.com/';
		$activity = array();
		$this->snoopy->fetch($this->fetchurl);
		$html = $this->snoopy->results;
		$this->dom->load($html);
		if ($this->dom->find('div[id=ind_mail]', 0)) {
			foreach ($this->dom->find('div[id=ind_mail]', 0)->find('div[class=ind_brand]') as $ind_brand) {
				if ($ind_brand) {
					//活动链接
					$brandurl = $ind_brand->find('div[class=brand_list]', 0)->find('a', 0)->href;
					//图片地址
					$activity['url'] = $brandurl ? $this->fetchurl.$brandurl : '';
					if ($ind_brand->find('div[class=brand_list]', 0)->find('a', 0)) {
						$act_img = $ind_brand->find('div[class=brand_list]', 0)->find('a', 0)->find('img', 0)->src;
					}else{
						$act_img = '';
						continue;
					}

					
					if ($act_img) {
						if ($act_img == 'tr.gif') {
							//如果应用了延迟加载图片 则加载延迟加载链接
							$imageurl = $ind_brand->find('div[class=brand_list]', 0)->find('a', 0)->find('img', 0)->lazy_src;
						}else{
							$imageurl = $act_img;
						}
					}else{
						$imageurl = '';
					}
					$activity['imageurl'] = $imageurl;
					//活动描述
					$title = $ind_brand->find('div[class=brand_list]', 0)->find('a', 0)->find('img', 0)->alt;
					$activity['title'] = $title ? $title : '';
					$brand = $ind_brand->find('span[class=ind_bra_n]', 0)->find('a', 0)->plaintext;
		//			$activity['brand'] = $brand;
					if ($title) {
						$vipdate_arr = explode('_', $title);
						$time = explode('到', $vipdate_arr[1]);
						$temptime = str_replace(array('年','月','日'), array('-','-',''), date('Y', now()).'年'.$time[0]);
						$activity['starttime'] = strtotime($temptime);
						$temptime = str_replace(array('年','月','日'), array('-','-',''), date('Y', now()).'年'.$time[1]);
						$activity['endtime'] = strtotime($temptime);
						//折扣
						$preg = '/[-+]?[0-9]*\.?[0-9]+/';
						if(preg_match($preg, $vipdate_arr[2], $matches)){
							$activity['discount'] = $matches[0];
						}
						$activity['dateline'] = now();
					}
					$query = $this->db->get_where($this->activity_table, array('url' => $activity['url']));
					$exist_act = $query->row_array();
					if ($exist_act) {
						$logstr = "id:".$exist_act['aid'].";url:".$exist_act['url'].";msg:已经存在抓取失败;";
						writelog('fetch_vipshop', $logstr);					
					}else{
						$id = $this->insert($activity);
						//抓取商品信息
						if ($activity['url'] && $id) {
							$this->snoopy->fetch($activity['url']);
							$goodshtml = $this->snoopy->results;
							$this->load->library('simple_html_dom', '', 'goodsdom');
							$this->goodsdom->load($goodshtml);
							if ($this->goodsdom->find('div[id=merchandise]', 0)) {
								foreach ($this->goodsdom->find('div[id=merchandise]', 0)->find('div[class=mchd_list]') as $key=>$goodsinfo) {
									//只抓取100个商品
									if ($key == $this->fetch_goods_num) {
										break;
									}
									$goods = array();
									$goods['aid'] = $id;
									//商品链接
									$goodsurl = $goodsinfo->find('a', 0)->href;
									$goods['url'] = $goodsurl ? $this->fetchurl.$goodsurl : '';
									//图片地址

									if ($goodsinfo->find('a', 0)->find('img', 0)->src) {
										if ($goodsinfo->find('a', 0)->find('img', 0)->src == 'tr.gif') {
											//如果应用了延迟加载图片 则加载延迟加载链接
											$imageurl = $goodsinfo->find('a', 0)->find('img', 0)->lazy_src;
										}else{
											$imageurl = $goodsinfo->find('a', 0)->find('img', 0)->src;
										}
									}else{
										$imageurl = '';
									}
									$goods['imageurl'] = $imageurl;
									//货物名称
									$goods['name'] = $goodsinfo->find('a[class=mchd_price]', 0)->title;
									//实际价格
									$price = $goodsinfo->find('a[class=mchd_price]', 0)->find('span[class=zhekou]', 0)->find('span', 0)->plaintext;
									$goods['price'] = $price ? str_replace('￥','',$price) : '';
									//市场价格
									$marketprice = $goodsinfo->find('a[class=mchd_price]', 0)->find('span[class=jiage]', 0)->find('span', 0)->plaintext;
									$goods['marketprice'] = $marketprice ? $marketprice : '';

									//折扣
									$discount = $goodsinfo->find('a[class=mchd_price]', 0)->find('span[class=zhekou]', 0)->plaintext;
									$preg = '/\(([-+]?[0-9]*\.?[0-9]+).+\)?/';
									if(preg_match($preg, $discount, $matches)){
										$discount = $matches[1];
									}else{
										$discount = round(($goods['price'] / $goods['marketprice'])*10, 1);
									}
									$goods['discount'] = $discount ? $discount : '';
									$goods['dateline'] = now();
									$goodsid = $this->insert_goods($goods);
									if ($goodsid) {
										$logstr = "aid:".$goods['aid'].";goodsid:".$goodsid.";url:".$goods['url'].";msg:商品抓取成功;";
										writelog('fetch_vipshop', $logstr);
									}else {
										$logstr = "aid:".$goods['aid'].";url:".$goods['url'].";msg:商品抓取失败;";
										writelog('fetch_vipshop', $logstr);
									}

									//释放变量
									unset($this->goodsdom);
								}
							}

						}

						if ($id) {
							$logstr = "aid:".$id.";url:$brandurl;msg:活动抓取成功;";
							writelog('fetch_vipshop', $logstr);
						}else {
							$logstr = "url:$brandurl;msg:活动抓取失败;";
							writelog('fetch_vipshop', $logstr);
						}					
					}
				}
			}
			return true;		
		}else{
			return false;
		}


		
	}
	
	function insert($data){
		if (is_array($data)) {
			$this->db->insert($this->activity_table, $data);
			return $this->db->insert_id();
		}
		return false;
	}
	
	function insert_goods($data){
		if (is_array($data)) {
			$this->db->insert($this->goods_table, $data);
			return $this->db->insert_id();
		}
		return false;
	}

	
	
	function get_activitys($num = '', $offset = ''){

		$this->db->order_by('starttime', 'desc');
		
		if (empty($num) || $offset ==='') {
			$this->db->limit('100');
			$query = $this->db->get($this->activity_table);	
		}else{
			$query = $this->db->get($this->activity_table, $num, $offset);				
		}
		
		foreach ($query->result_array() as $row) {
			$row['ison'] = $row['status'] ? '已上架' : '未上架';
			$activitys[] = $row;
		}
		return $activitys;
	}
}

?>